<?php
declare(strict_types=1);

namespace hangpu8\hpcms\controller;

use hangpu8\hpcms\Utils\Json;
use think\exception\ValidateException;

class Admin extends Base
{
    /**
     * 列表
     *
     * @return void
     */
    public function index()
    {
        $service = new \hangpu8\hpcms\service\Admin;
        $list = $service->getlist();
        $builder = new \hangpu8\hpcms\Utils\Builder\ListBuilder;
        $data = $builder
            ->addColumn('rightButtonList', '操作', [
                'type'              => 'template',
                'template'          => 'rightButtonList'
            ])
            ->addTopButton('add', '添加', [
                'title'             => '添加数据',
                'api'               => 'hangpu8/Admin/add',
            ])
            ->addRightButton('edit', '修改',[
                'api'               => 'hangpu8/Admin/edit',
                'title'             => '修改',
            ])
            ->addRightButton('delete', '删除', [
                'api'               => 'hangpu8/Admin/del',
                'method'            => 'delete',
                'title'             => '确认要删除该数据吗？',
                'modalType'         => 'confirm',
                'width'             => '600',
                'okText'            => '确认删除',
                'cancelText'        => '取消操作',
                'content'           => '<p>删除该数据不可恢复</p>',
            ])
            ->addColumn('id', '序号',[
                'width'             => '80',
            ])
            ->addColumn('ctime', '添加时间',[
                'width'             => '160',
            ])
            ->addColumn('admin_role', '所属角色')
            ->addColumn('headimg', '头 像')
            ->addColumn('username', '账 号')
            ->addColumn('nickname', '昵 称')
            ->addColumn('status_text', '状 态')
            ->addColumn('last_login_ip', '最近登录IP')
            ->addColumn('last_login_time', '最近登录时间')
            ->setData($list['data'])
            ->create();
        return Json::successful($data);
    }

    /**
     * 添加数据
     *
     * @return void
     */
    public function add()
    {
        $service = new \hangpu8\hpcms\service\Admin;
        if (request()->isPost())
        {
            $post = request()->post();
            try {
                validate(\hangpu8\hpcms\validate\Admin::class)
                ->scene('add')
                ->check($post);
            } catch (ValidateException $e) {
                list($code, $msg) = explode("\n", $e->getMessage());
                return Json::fail($msg,(int)$code);
            }
            if (isset($post['password']) && $post['password'])
            {
                $post['password'] = md5($post['password']);
            }
            try {
                $service->create($post);
                return Json::successful([],'操作成功');
            } catch (\Throwable $e) {
                return Json::fail($e->getMessage(),$e->getCode());
            }
        }else{
            $roleService = new \hangpu8\hpcms\service\AdminRole;
            $roleList = $roleService->getRoleOptions();
            $formBuilder = new \hangpu8\hpcms\Utils\Builder\FormService();
            $data = $formBuilder
            ->setMethod('post')
            ->addRow('role_id','select','所属角色','',[
                'options'       => $roleList,
            ])
            ->addRow('username','input','登录账号')
            ->addRow('password','input','登录密码')
            ->addRow('nickname','input','用户昵称')
            ->addRow('status','radio','当前状态',1,[
                'options'       => [
                    [
                        'label' => '锁定',
                        'value' => 0,
                    ],
                    [
                        'label' => '正常',
                        'value' => 1,
                    ],
                ],
            ])
            ->create();
            return Json::successful($data);
        }
    }

    /**
     * 修改数据
     *
     * @param [type] $path
     * @return void
     */
    public function edit($id)
    {
        $service = new \hangpu8\hpcms\service\Admin;
        if (request()->isPut())
        {
            $post = request()->post();
            try {
                validate(\hangpu8\hpcms\validate\Admin::class)
                ->scene('edit')
                ->check($post);
            } catch (ValidateException $e) {
                list($code, $msg) = explode("\n", $e->getMessage());
                return Json::fail($msg,(int)$code);
            }
            if ($post['password'])
            {
                $post['password'] = md5($post['password']);
            }else{
                unset($post['password']);
            }
            try {
                $service->saveUpdate($post,['id'=> $id]);
                return Json::successful([],'操作成功');
            } catch (\Throwable $e) {
                return Json::fail($e->getMessage(),$e->getCode());
            }
        }else{
            $form = $service->getForm(['id'=> $id]);
            $roleService = new \hangpu8\hpcms\service\AdminRole;
            $roleList = $roleService->getRoleOptions();
            $formBuilder = new \hangpu8\hpcms\Utils\Builder\FormService();
            $data = $formBuilder
            ->setMethod('put')
            ->addRow('role_id','select','所属角色','',[
                'options'       => $roleList,
            ])
            ->addRow('username','input','登录账号')
            ->addRow('password','input','登录密码')
            ->addRow('nickname','input','用户昵称')
            ->addRow('status','radio','当前状态',1,[
                'options'       => [
                    [
                        'label' => '锁定',
                        'value' => 0,
                    ],
                    [
                        'label' => '正常',
                        'value' => 1,
                    ],
                ],
            ])
            ->setFormData($form)
            ->create();
            return Json::successful($data);
        }
    }

    /**
     * 删除数据
     *
     * @param [type] $path
     * @return void
     */
    public function del($id)
    {
        $service = new \hangpu8\hpcms\service\Admin;
        try {
            $service->del($id);
            return Json::successful([],'删除成功');
        } catch (\Throwable $e) {
            return Json::fail($e->getMessage(),$e->getCode());
        }
    }
}